package com.hzqy.service.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.JsonUtils;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.product.ProductDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmCatalogContentVo;
import com.hzqy.web.vo.CmPicturesVo;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.SysProductOrgVo;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.PmsUserVo;

@Service
public class ProductServiceBean extends GenericServiceBean<PmsUserVo, ProductDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private ProductDao productDao;
	@Resource
	private PictureDao pictureDao;
	
	@Override
	public ProductDao getEntityDao(){
		return productDao;
	}
	
	public Map<String,Object> selectProductList(SysProductVo sysProductVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = productDao.findProductCount(sysProductVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_sp_id", sysProductVo.getF_sp_id());
			map.put("f_sp_name", sysProductVo.getF_sp_name());
			map.put("f_sp_status", sysProductVo.getF_sp_status());
			map.put("f_sp_parentid", sysProductVo.getF_sp_parentid());
			map.put("f_spo_orgid", sysProductVo.getF_spo_orgid());
			map.put("f_sp_priority", sysProductVo.getF_sp_priority());
			List<SysProductVo> results = productDao.selectProductList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<SysProductVo>());
		}
		return map;
	}

	public List<SysProductVo> selectProductList(SysProductVo sysProductVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_sp_id", sysProductVo.getF_sp_id());
		map.put("f_sp_name", sysProductVo.getF_sp_name());
		map.put("f_sp_status", sysProductVo.getF_sp_status());
		map.put("f_sp_parentid", sysProductVo.getF_sp_parentid());
		List<SysProductVo> results = productDao.selectProductList(map);
		return results;
	}
	
	public int deleteProduct(int f_sp_id) {
		productDao.deleteProduct(f_sp_id);
		return 1;
	}
	
	public int insertProduct(SysProductVo spv,String picJson, String[] companyId) {
		int flag = 0;
		if(spv==null || spv.getF_sp_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			productDao.insertProduct(spv);
			int f_sp_id = spv.getF_sp_id();
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_sp_id);
				pictureDao.insertPic(cmPicturesVo);
			}
			if(companyId != null){
				for(int i=0;i<companyId.length;i++){
					SysProductOrgVo productorg = new SysProductOrgVo();
					productorg.setF_spo_orgid(Integer.valueOf(companyId[i]));
					productorg.setF_spo_productid(f_sp_id);
					productDao.insertProductOrg(productorg);
				}
			}
			return 1;
		}
		return flag;
	}
	
	public SysProductVo selectProduct(int f_sp_id) {
		return productDao.selectProduct(f_sp_id);
	}
	
	public int updateProduct(SysProductVo spv,String picJson,String picType, String[] companyId) {
		int flag = 0;
		if(spv==null || spv.getF_sp_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			List<CmPicturesVo> picList = JsonUtils.jsonToPicVo(picJson);
			String listPicId = JsonUtils.jsonToPicIdList(picJson);
			int f_cv_relationid = spv.getF_sp_id();
			Map<String,Object> picMap = new HashMap<String, Object>();
			picMap.put("listPicId", listPicId);
			picMap.put("f_cp_relationid", f_cv_relationid);
			picMap.put("picType", picType);
			
			productDao.updateProduct(spv);
			pictureDao.deletePic(picMap);
			for(int i=0;i<picList.size();i++) {
				CmPicturesVo cmPicturesVo = new CmPicturesVo();
				cmPicturesVo = picList.get(i);
				cmPicturesVo.setF_cp_relationid(f_cv_relationid);
				pictureDao.insertPic(cmPicturesVo);
			}
			SysProductOrgVo sysProductOrgVo = new SysProductOrgVo();
			sysProductOrgVo.setF_spo_productid(f_cv_relationid);
			productDao.deleteProductOrg(sysProductOrgVo);
			if(companyId != null){
				for(int i=0;i<companyId.length;i++){
					SysProductOrgVo productOrg = new SysProductOrgVo();
					productOrg.setF_spo_orgid(Integer.valueOf(companyId[i]));
					productOrg.setF_spo_productid(f_cv_relationid);
					productDao.insertProductOrg(productOrg);
				}
			}
			
			return 1;
		}
		return flag;
	}

	public List<PmsOrgVo> selectProductOrg(int f_sp_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_sp_id", f_sp_id);
		map.put("count", 0);
		return productDao.selectProductOrg(map);
	}

	public List<PmsOrgVo> selectProductOrgList(int f_sp_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_sp_id", f_sp_id);
		map.put("count", 0);
		return productDao.selectProductOrgList(map);
	}
	
	public List<PmsOrgVo> selectProductOrgBind(int f_sp_id,String f_po_name, String f_po_address, String f_po_stars, String f_po_price, int min , int max,int bind_status,float minScore , float maxScore,String labelId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_sp_id", f_sp_id);
		map.put("f_po_name", f_po_name);
		map.put("f_po_stars", f_po_stars);
		map.put("f_po_price", f_po_price);
		map.put("min", min);
		map.put("max", max);
		map.put("minScore", minScore);
		map.put("maxScore", maxScore);
		map.put("labelId", labelId);
		int count = 1;//标签条件的个数
		if(!"".equals(labelId.trim())&&labelId!=null&&labelId.indexOf(",")!=-1){
			count = labelId.split(",").length;
		}
		map.put("count", count);
		int sign = 0;
		if(f_po_address!=""&&f_po_address!=null){
			String areaArr[] = f_po_address.split("-");
			if(areaArr.length==1){
				f_po_address = areaArr[0]+"-";//如果城市为空时 只取省份
			}else if(areaArr.length==2){//地市为空时
				
			}else if(areaArr.length==3){
				sign = 1;
			}
		}
		map.put("f_po_address", f_po_address);
		map.put("sign", sign);//1代表地区查询用=号 0代表模糊查询
		List<PmsOrgVo> orgVo = null;
		if(bind_status==1){//已绑定
			orgVo = productDao.selectProductOrg(map);
		}else if(bind_status==2){//未绑定
			orgVo = productDao.selectProductOrgList(map);
		}
		return orgVo;
	}
	
	public int insertProductOrgBind(int f_sp_id,String ids,int bind_status) {
		int flag = 0;
		if(f_sp_id <= 0 || ids==null || ids.equals("")) {
			flag = -1;
		}
		if(flag == 0) {
			String[] idArr = ids.split(",");
			for(int i=0; i<idArr.length;i++) {
				int f_spo_orgid = Integer.valueOf(idArr[i]);
				SysProductOrgVo sysProductOrgVo = new SysProductOrgVo();
				sysProductOrgVo.setF_spo_productid(f_sp_id);
				sysProductOrgVo.setF_spo_orgid(f_spo_orgid);
				if(bind_status==1){//插入
					productDao.insertProductOrg(sysProductOrgVo);
				}else if(bind_status==2){//删除
					productDao.deleteProductOrg(sysProductOrgVo);
				}
			}
			flag = 1;
		}
		return flag;
	}
}
